package Main;

import com.alibaba.fastjson.JSON;
import com.formdev.flatlaf.FlatIntelliJLaf;
import ui.HomePage;
import util.GsonUils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.*;

/**
 * @author zhongwen
 * @description 项目的唯一入口
 * @date 2021-12-08 21:33
 */
public class Main {
    public static ConfigurationBean configurationBean;
    public static void main(String[] args) {
        loadConfig();
        try {
            UIManager.setLookAndFeel(new FlatIntelliJLaf());
            UIManager.put("Component.disabledBorderColor", Color.white);
            UIManager.put("Component.focusColor", Color.white);
            UIManager.put("JTabbedPane.hasFullBorder", false);
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();
        }
        new HomePage().init();
        getEnglish();
    }
    /**
     * 读入配置文件到内存
     */
    private static void loadConfig(){
        File configFile = new File("config.json");
        if(configFile.exists()){
            try {
                StringBuilder sb = new StringBuilder("");
                String s;
                BufferedReader reader = new BufferedReader(new FileReader(configFile));
                while ((s = reader.readLine()) != null){
                    sb.append(s);
                }
                configurationBean = JSON.parseObject(sb.toString(), ConfigurationBean.class);
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else{
            configurationBean = new ConfigurationBean();
            configurationBean.setFont(new Font("宋体",Font.PLAIN,18));
            configurationBean.setNowPath("C:");
            configurationBean.setImageWidthAndHeight(18);
            configurationBean.setWorkPath(configFile.getAbsolutePath());
            configurationBean.setJdkPath(new File("C:"));
            GsonUils.objectToString(configurationBean);
        }
    }

    /**
     * 模拟键盘按下输入语言切换为English
     */
    private static void getEnglish(){
        /*
         * 线程需要睡眠一会，因为Swing的面板全部绘制成功需要一定时间。在不睡眠一定时长的情况下，
         * 切换语言的键按完后，Swing面板才绘制完成，这时JTextArea的子类会获取系统默认语言。
         */

        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_SHIFT);
            robot.keyRelease(KeyEvent.VK_SHIFT);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
}
